let app = getApp();
import { supplierApply, getSupplierData, getGoodsAreas } from "../../../api/index";
import { uploadImg } from "../../../utils/request";
import { disposeData } from "../../../utils/outData";
import {
  checkName,
  checkPhone,
  validateIdCard,
  checkRegisterCode,
} from "../../../utils/validate";
Page({
  data: {
    people: '',
    contact_tel: '',
    shopname: '',
    shoptype: '',
    photo: '',
    faptype: '',
    mechanism: '',
    companyid: '',
    shopcity: '',
    company_area: '',
    area_id: '',
    company: '',
    shopcard: '',
    doorphoto: '',
    seal_url: '',
    faname: '',
    idcard: '',
    faidcard: '',
    faidcard_back: '',
    showFaptype: false,
    popupData: {},
    fapList: [],
    shop_index: -1,
    areasList: [],
    relationId: '' // 分销产品供应商入驻关联id
  },
  onLoad() {
    this.setData({
      relationId: disposeData().relationId || ''
    });
    this.getPopupDats()
    this.getCitys()
  },
  getPopupDats() {
    getSupplierData().then(res => {
      let arr1 = Object.keys(res.fap_type).map((key, index) => ({ name: res.fap_type[key], id: index + 1 }))
      let arr2 = Object.keys(res.risk_type).map(key => ({ name: res.risk_type[key] }))
      // arr1.map((item, index) => {
      //   item["checked"] = false;
      // });
      // arr2.map((item, index) => {
      //   item["checked"] = false;
      // });
      this.setData({
        popupData: res,
        fapList: arr1
      })
    })
  },
  onPeople(e) {
    this.setData({ people: e.detail.value })
  },
  onContactTel(e) {
    this.setData({ contact_tel: e.detail.value })
  },
  onShopname(e) {
    this.setData({ shopname: e.detail.value })
  },
  onMechanism(e) {
    this.setData({ mechanism: e.detail.value })
  },
  onCompanyid(e) {
    this.setData({ companyid: e.detail.value })
  },
  onCompany(e) {
    this.setData({ company: e.detail.value })
  },
  onFaname(e) {
    this.setData({ faname: e.detail.value })
  },
  onIdcard(e) {
    this.setData({ idcard: e.detail.value })
  },
  selectShoptype(e) {
    this.setData({
      shop_index: e.detail.value,
      shoptype: this.data.popupData.category[e.detail.value].id.toString()
    });
  },
  selectFaptype(e) {
    this.setData({ showFaptype: true });
  },
  onSubmit_faptype(e) {
    this.setData({
      showFaptype: false,
      faptype: e.detail.value.libs,
    });
  },
  onReset_faptype(e) {
    this.setData({ showFaptype: false });
  },
  getCitys() {
    getGoodsAreas().then(res => {
      this.setData({
        areasList: res[0].sub
      })
    })
  },
  getList(type) {
    const { areasList } = this.data;
    let list = [];
    for (let p of areasList) {
      if (type == '1' && p.id == 7459) {
        continue;
      }
      let obj = {
        name: p.name,
        subList: []
      }
      if (p.sub) {
        for (let c of p.sub) {
          let obj1 = {
            name: c.name,
            subList: []
          }
          if (c.sub) {
            for (let ct of c.sub) {
              obj1.subList.push({
                name: ct.name,
              })
            }
          }
          obj.subList.push(obj1);
        }
      }
      list.push(obj);
    }
    return list;
  },
  selectShopcity(e) {
    const type = e.target.dataset.type;
    const list = this.getList(type);
    let selectTitle = type == '1' ? '公司所在地区' : '当前经营位置'
    my.multiLevelSelect({
      title: selectTitle,//级联选择标题
      list: list,
      success: (res) => {
        if (res.success) {
          let area = "", area_id = "";
          res.result.forEach((element, index) => {
            if (index > 0) {
              area += '-' + element.name;
            } else {
              area += element.name;
            }
          });
          for (let p of this.data.areasList) {
            if (p.name == res.result[0].name) {
              for (let c of p.sub) {
                if (c.name == res.result[1].name) {
                  area_id = c.id;
                }
              }
            }
          }
          if (type == 1) {
            this.setData({
              company_area: area,
              area_id
            })
          } else {
            this.setData({
              shopcity: area
            })
          }
        }
      }
    });
  },
  onPopupClose() {
    this.setData({
      showFaptype: false
    });
  },
  uploadPhoto(e) {
    const that = this;
    let type = e.currentTarget.dataset.type
    switch (type) {
      case "photo":
        uploadImg(function (url) {
          if (url) {
            that.setData({ photo: url })
          }
        });
        break;
      case "shopcard":
        uploadImg(function (url) {
          if (url) {
            that.setData({ shopcard: url })
          }
        });
        break;
      case "doorphoto":
        uploadImg(function (url) {
          if (url) {
            that.setData({ doorphoto: url })
          }
        });
        break;
      case "seal_url":
        uploadImg(function (url) {
          if (url) {
            that.setData({ seal_url: url })
          }
        });
        break;
      case "faidcard":
        uploadImg(function (url) {
          if (url) {
            that.setData({ faidcard: url })
          }
        });
        break;
      case "faidcard_back":
        uploadImg(function (url) {
          if (url) {
            that.setData({ faidcard_back: url })
          }
        });
        break;

      default:
        break;
    }
  },
  showMsg(msg) {
    my.showToast({
      type: 'none',
      duration: 1000,
      content: msg
    });
  },
  submitBtn() {
    const that = this
    const { people, contact_tel, shopname, shoptype, photo, faptype, mechanism, companyid, shopcity, 
      company_area, area_id, company, shopcard, doorphoto, seal_url, faname, idcard, faidcard, faidcard_back, relationId } = that.data
    if (!checkName(people)) {
      return;
    }
    if (!checkPhone(contact_tel)) {
      return;
    }
    if (!shopname) {
      this.showMsg('店铺名称不能为空')
      return;
    }
    if (!shoptype) {
      this.showMsg('请选择经营项目')
      return;
    }
    if (!photo) {
      this.showMsg('请上传店铺头像')
      return;
    }
    if (!validateIdCard(idcard)) {
      return;
    }
    if (!checkRegisterCode(companyid)) {
      return;
    }
    let obj = {
      relationId,
      people, contact_tel, shopname, shoptype, photo, 
      mechanism, companyid, shopcity, company_area, area_id, company, shopcard,
      doorphoto, seal_url, faname, idcard, faidcard, faidcard_back,
      faptype: faptype.length == 2 ? '1,2' : faptype[0] == '普通发票' ? '1' : '2'
    }
    for (var key in obj) {
      if (obj[key] == '') {
        this.showMsg('请把公司信息补充完整')
        return
      }
    }
    supplierApply(obj).then(res => {
      this.showMsg('提交成功')
      my.navigateTo({
        url: '../supplierSuccess/supplierSuccess'
      });
    })
  }
});
